package com.lemeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lemeng.pojo.Admin;
import com.lemeng.service.AdminService;
import com.lemeng.service.BrandService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class UserDetailServiceImpl implements UserDetailsService {


    @Reference
    private AdminService adminService;

    @Reference
    BrandService brandService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("loginName",s);
        hashMap.put("status","1");
        List<Admin> list = adminService.findList(hashMap);

        if (list==null||list.size()==0){
            return null;
        }

        ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new User(s,list.get(0).getPassword(),authorities);
    }
}
